fix for broken categories on preview
[lhc/web/wiklou.git] / includes / OutputPage.php
1 <?php
2 /**
3 * @package MediaWiki
4 */
5
6 /**
7 * This is not a valid entry point, perform no further processing unless MEDIAWIKI is defined
8 */
9 if( defined( 'MEDIAWIKI' ) ) {
10
11 # See design.txt
12
13 if($wgUseTeX) require_once( 'Math.php' );
14
15 /**
16 * @todo document
17 * @package MediaWiki
18 */
19 class OutputPage {
20 var $mHeaders, $mMetatags, $mKeywords;
21 var $mLinktags, $mPagetitle, $mBodytext, $mDebugtext;
22 var $mHTMLtitle, $mRobotpolicy, $mIsarticle, $mPrintable;
23 var $mSubtitle, $mRedirect, $mStatusCode;
24 var $mLastModified, $mETag, $mCategoryLinks;
25 var $mScripts, $mLinkColours;
26
27 var $mSuppressQuickbar;
28 var $mOnloadHandler;
29 var $mDoNothing;
30 var $mContainsOldMagic, $mContainsNewMagic;
31 var $mIsArticleRelated;
32 var $mParserOptions;
33 var $mShowFeedLinks = false;
34 var $mEnableClientCache = true;
35 var $mArticleBodyOnly = false;
36
37 /**
38 * Constructor
39 * Initialise private variables
40 */
41 function OutputPage() {
42 $this->mHeaders = $this->mMetatags =
43 $this->mKeywords = $this->mLinktags = array();
44 $this->mHTMLtitle = $this->mPagetitle = $this->mBodytext =
45 $this->mRedirect = $this->mLastModified =
46 $this->mSubtitle = $this->mDebugtext = $this->mRobotpolicy =
47 $this->mOnloadHandler = '';
48 $this->mIsArticleRelated = $this->mIsarticle = $this->mPrintable = true;
49 $this->mSuppressQuickbar = $this->mPrintable = false;
50 $this->mLanguageLinks = array();
51 $this->mCategoryLinks = array() ;
52 $this->mDoNothing = false;
53 $this->mContainsOldMagic = $this->mContainsNewMagic = 0;
54 $this->mParserOptions = ParserOptions::newFromUser( $temp = NULL );
55 $this->mSquidMaxage = 0;
56 $this->mScripts = '';
57 $this->mETag = false;
58 $this->mRevisionId = null;
59 }
60
61 function addHeader( $name, $val ) { array_push( $this->mHeaders, $name.': '.$val ) ; }
62 function redirect( $url, $responsecode = '302' ) { $this->mRedirect = $url; $this->mRedirectCode = $responsecode; }
63 function setStatusCode( $statusCode ) { $this->mStatusCode = $statusCode; }
64
65 # To add an http-equiv meta tag, precede the name with "http:"
66 function addMeta( $name, $val ) { array_push( $this->mMetatags, array( $name, $val ) ); }
67 function addKeyword( $text ) { array_push( $this->mKeywords, $text ); }
68 function addScript( $script ) { $this->mScripts .= $script; }
69 function getScript() { return $this->mScripts; }
70
71 function setETag($tag) { $this->mETag = $tag; }
72 function setArticleBodyOnly($only) { $this->mArticleBodyOnly = $only; }
73 function getArticleBodyOnly($only) { return $this->mArticleBodyOnly; }
74
75 function addLink( $linkarr ) {
76 # $linkarr should be an associative array of attributes. We'll escape on output.
77 array_push( $this->mLinktags, $linkarr );
78 }
79
80 function addMetadataLink( $linkarr ) {
81 # note: buggy CC software only reads first "meta" link
82 static $haveMeta = false;
83 $linkarr['rel'] = ($haveMeta) ? 'alternate meta' : 'meta';
84 $this->addLink( $linkarr );
85 $haveMeta = true;
86 }
87
88 /**
89 * checkLastModified tells the client to use the client-cached page if
90 * possible. If sucessful, the OutputPage is disabled so that
91 * any future call to OutputPage->output() have no effect. The method
92 * returns true iff cache-ok headers was sent.
93 */
94 function checkLastModified ( $timestamp ) {
95 global $wgCachePages, $wgUser;
96 if ( !$timestamp || $timestamp == '19700101000000' ) {
97 wfDebug( "CACHE DISABLED, NO TIMESTAMP\n" );
98 return;
99 }
100 if( !$wgCachePages ) {
101 wfDebug( "CACHE DISABLED\n", false );
102 return;
103 }
104 if( $wgUser->getOption( 'nocache' ) ) {
105 wfDebug( "USER DISABLED CACHE\n", false );
106 return;
107 }
108
109 $timestamp=wfTimestamp(TS_MW,$timestamp);
110 $lastmod = wfTimestamp( TS_RFC2822, max( $timestamp, $wgUser->mTouched ) );
111
112 if( !empty( $_SERVER['HTTP_IF_MODIFIED_SINCE'] ) ) {
113 # IE sends sizes after the date like this:
114 # Wed, 20 Aug 2003 06:51:19 GMT; length=5202
115 # this breaks strtotime().
116 $modsince = preg_replace( '/;.*$/', '', $_SERVER["HTTP_IF_MODIFIED_SINCE"] );
117 $modsinceTime = strtotime( $modsince );
118 $ismodsince = wfTimestamp( TS_MW, $modsinceTime ? $modsinceTime : 1 );
119 wfDebug( "-- client send If-Modified-Since: " . $modsince . "\n", false );
120 wfDebug( "-- we might send Last-Modified : $lastmod\n", false );
121 if( ($ismodsince >= $timestamp ) && $wgUser->validateCache( $ismodsince ) ) {
122 # Make sure you're in a place you can leave when you call us!
123 header( "HTTP/1.0 304 Not Modified" );
124 $this->mLastModified = $lastmod;
125 $this->sendCacheControl();
126 wfDebug( "CACHED client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
127 $this->disable();
128 @ob_end_clean(); // Don't output compressed blob
129 return true;
130 } else {
131 wfDebug( "READY client: $ismodsince ; user: $wgUser->mTouched ; page: $timestamp\n", false );
132 $this->mLastModified = $lastmod;
133 }
134 } else {
135 wfDebug( "client did not send If-Modified-Since header\n", false );
136 $this->mLastModified = $lastmod;
137 }
138 }
139
140 function getPageTitleActionText () {
141 global $action;
142 switch($action) {
143 case 'edit':
144 case 'delete':
145 case 'protect':
146 case 'unprotect':
147 case 'watch':
148 case 'unwatch':
149 // Display title is already customized
150 return '';
151 case 'history':
152 return wfMsg('history_short');
153 case 'submit':
154 // FIXME: bug 2735; not correct for special pages etc
155 return wfMsg('preview');
156 case 'info':
157 return wfMsg('info_short');
158 default:
159 return '';
160 }
161 }
162
163 function setRobotpolicy( $str ) { $this->mRobotpolicy = $str; }
164 function setHTMLTitle( $name ) {$this->mHTMLtitle = $name; }
165 function setPageTitle( $name ) {
166 global $action, $wgContLang;
167 $name = $wgContLang->convert($name, true);
168 $this->mPagetitle = $name;
169 if(!empty($action)) {
170 $taction = $this->getPageTitleActionText();
171 if( !empty( $taction ) ) {
172 $name .= ' - '.$taction;
173 }
174 }
175
176 $this->setHTMLTitle( wfMsg( 'pagetitle', $name ) );
177 }
178 function getHTMLTitle() { return $this->mHTMLtitle; }
179 function getPageTitle() { return $this->mPagetitle; }
180 function setSubtitle( $str ) { $this->mSubtitle = /*$this->parse(*/$str/*)*/; } // @bug 2514
181 function getSubtitle() { return $this->mSubtitle; }
182 function isArticle() { return $this->mIsarticle; }
183 function setPrintable() { $this->mPrintable = true; }
184 function isPrintable() { return $this->mPrintable; }
185 function setSyndicated( $show = true ) { $this->mShowFeedLinks = $show; }
186 function isSyndicated() { return $this->mShowFeedLinks; }
187 function setOnloadHandler( $js ) { $this->mOnloadHandler = $js; }
188 function getOnloadHandler() { return $this->mOnloadHandler; }
189 function disable() { $this->mDoNothing = true; }
190
191 function setArticleRelated( $v ) {
192 $this->mIsArticleRelated = $v;
193 if ( !$v ) {
194 $this->mIsarticle = false;
195 }
196 }
197 function setArticleFlag( $v ) {
198 $this->mIsarticle = $v;
199 if ( $v ) {
200 $this->mIsArticleRelated = $v;
201 }
202 }
203
204 function isArticleRelated() { return $this->mIsArticleRelated; }
205
206 function getLanguageLinks() { return $this->mLanguageLinks; }
207 function addLanguageLinks($newLinkArray) {
208 $this->mLanguageLinks += $newLinkArray;
209 }
210 function setLanguageLinks($newLinkArray) {
211 $this->mLanguageLinks = $newLinkArray;
212 }
213
214 function getCategoryLinks() {
215 return $this->mCategoryLinks;
216 }
217
218 /**
219 * Add an array of categories, with names in the keys
220 */
221 function addCategoryLinks($categories) {
222 global $wgUser, $wgLinkCache, $wgContLang;
223
224 # Add the links to the link cache in a batch
225 $arr = array( NS_CATEGORY => $categories );
226 $lb = new LinkBatch;
227 $lb->setArray( $arr );
228 $lb->execute( $wgLinkCache );
229
230 $sk =& $wgUser->getSkin();
231 foreach ( $categories as $category => $arbitrary ) {
232 $title = Title::makeTitleSafe( NS_CATEGORY, $category );
233 $text = $wgContLang->convertHtml( $title->getText() );
234 $this->mCategoryLinks[] = $sk->makeLinkObj( $title, $text );
235 }
236 }
237
238 function setCategoryLinks($categories) {
239 $this->mCategoryLinks = array();
240 $this->addCategoryLinks($categories);
241 }
242
243 function suppressQuickbar() { $this->mSuppressQuickbar = true; }
244 function isQuickbarSuppressed() { return $this->mSuppressQuickbar; }
245
246 function addHTML( $text ) { $this->mBodytext .= $text; }
247 function clearHTML() { $this->mBodytext = ''; }
248 function getHTML() { return $this->mBodytext; }
249 function debug( $text ) { $this->mDebugtext .= $text; }
250
251 /* @deprecated */
252 function setParserOptions( $options ) {
253 return $this->ParserOptions( $options );
254 }
255
256 function ParserOptions( $options = null ) {
257 return wfSetVar( $this->mParserOptions, $options );
258 }
259
260 /**
261 * Set the revision ID which will be seen by the wiki text parser
262 * for things such as embedded {{REVISIONID}} variable use.
263 * @param mixed $revid an integer, or NULL
264 * @return mixed previous value
265 */
266 function setRevisionId( $revid ) {
267 $val = is_null( $revid ) ? null : intval( $revid );
268 return wfSetVar( $this->mRevisionId, $val );
269 }
270
271 /**
272 * Convert wikitext to HTML and add it to the buffer
273 * Default assumes that the current page title will
274 * be used.
275 */
276 function addWikiText( $text, $linestart = true ) {
277 global $wgTitle;
278 $this->addWikiTextTitle($text, $wgTitle, $linestart);
279 }
280
281 function addWikiTextWithTitle($text, &$title, $linestart = true) {
282 $this->addWikiTextTitle($text, $title, $linestart);
283 }
284
285 function addWikiTextTitle($text, &$title, $linestart) {
286 global $wgParser;
287 $parserOutput = $wgParser->parse( $text, $title, $this->mParserOptions,
288 $linestart, true, $this->mRevisionId );
289 $this->addParserOutput( $parserOutput );
290 }
291
292 function addParserOutputNoText( &$parserOutput ) {
293 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
294 $this->addCategoryLinks( $parserOutput->getCategories() );
295 $this->addKeywords( $parserOutput );
296 if ( $parserOutput->getCacheTime() == -1 ) {
297 $this->enableClientCache( false );
298 }
299 }
300
301 function addParserOutput( &$parserOutput ) {
302 $this->addParserOutputNoText( $parserOutput );
303 $this->addHTML( $parserOutput->getText() );
304 }
305
306 /**
307 * Add wikitext to the buffer, assuming that this is the primary text for a page view
308 * Saves the text into the parser cache if possible
309 */
310 function addPrimaryWikiText( $text, $article, $cache = true ) {
311 global $wgParser, $wgParserCache, $wgUser;
312
313 $parserOutput = $wgParser->parse( $text, $article->mTitle,
314 $this->mParserOptions, true, true, $this->mRevisionId );
315
316 if ( $article && $parserOutput->getCacheTime() != -1 ) {
317 $wgParserCache->save( $parserOutput, $article, $wgUser );
318 }
319
320 $this->addParserOutput( $parserOutput );
321 }
322
323 /**
324 * Add the output of a QuickTemplate to the output buffer
325 * @param QuickTemplate $template
326 */
327 function addTemplate( &$template ) {
328 ob_start();
329 $template->execute();
330 $this->addHTML( ob_get_contents() );
331 ob_end_clean();
332 }
333
334 /**
335 * Parse wikitext and return the HTML. This is for special pages that add the text later
336 */
337 function parse( $text, $linestart = true ) {
338 global $wgParser, $wgTitle;
339 $parserOutput = $wgParser->parse( $text, $wgTitle, $this->mParserOptions,
340 $linestart, true, $this->mRevisionId );
341 return $parserOutput->getText();
342 }
343
344 /**
345 * @param $article
346 * @param $user
347 *
348 * @return bool
349 */
350 function tryParserCache( $article, $user ) {
351 global $wgParserCache;
352 $parserOutput = $wgParserCache->get( $article, $user );
353 if ( $parserOutput !== false ) {
354 $this->mLanguageLinks += $parserOutput->getLanguageLinks();
355 $this->addCategoryLinks( $parserOutput->getCategories() );
356 $this->addKeywords( $parserOutput );
357 $this->addHTML( $parserOutput->getText() );
358 $t = $parserOutput->getTitleText();
359 if( !empty( $t ) ) {
360 $this->setPageTitle( $t );
361 }
362 return true;
363 } else {
364 return false;
365 }
366 }
367
368 /**
369 * Set the maximum cache time on the Squid in seconds
370 * @param $maxage
371 */
372 function setSquidMaxage( $maxage ) {
373 $this->mSquidMaxage = $maxage;
374 }
375
376 /**
377 * Use enableClientCache(false) to force it to send nocache headers
378 * @param $state
379 */
380 function enableClientCache( $state ) {
381 return wfSetVar( $this->mEnableClientCache, $state );
382 }
383
384 function uncacheableBecauseRequestvars() {
385 global $wgRequest;
386 return $wgRequest->getText('useskin', false) === false
387 && $wgRequest->getText('uselang', false) === false;
388 }
389
390 function sendCacheControl() {
391 global $wgUseSquid, $wgUseESI;
392
393 if ($this->mETag)
394 header("ETag: $this->mETag");
395
396 # don't serve compressed data to clients who can't handle it
397 # maintain different caches for logged-in users and non-logged in ones
398 header( 'Vary: Accept-Encoding, Cookie' );
399 if( !$this->uncacheableBecauseRequestvars() && $this->mEnableClientCache ) {
400 if( $wgUseSquid && ! isset( $_COOKIE[ini_get( 'session.name') ] ) &&
401 ! $this->isPrintable() && $this->mSquidMaxage != 0 )
402 {
403 if ( $wgUseESI ) {
404 # We'll purge the proxy cache explicitly, but require end user agents
405 # to revalidate against the proxy on each visit.
406 # Surrogate-Control controls our Squid, Cache-Control downstream caches
407 wfDebug( "** proxy caching with ESI; {$this->mLastModified} **\n", false );
408 # start with a shorter timeout for initial testing
409 # header( 'Surrogate-Control: max-age=2678400+2678400, content="ESI/1.0"');
410 header( 'Surrogate-Control: max-age='.$wgSquidMaxage.'+'.$this->mSquidMaxage.', content="ESI/1.0"');
411 header( 'Cache-Control: s-maxage=0, must-revalidate, max-age=0' );
412 } else {
413 # We'll purge the proxy cache for anons explicitly, but require end user agents
414 # to revalidate against the proxy on each visit.
415 # IMPORTANT! The Squid needs to replace the Cache-Control header with
416 # Cache-Control: s-maxage=0, must-revalidate, max-age=0
417 wfDebug( "** local proxy caching; {$this->mLastModified} **\n", false );
418 # start with a shorter timeout for initial testing
419 # header( "Cache-Control: s-maxage=2678400, must-revalidate, max-age=0" );
420 header( 'Cache-Control: s-maxage='.$this->mSquidMaxage.', must-revalidate, max-age=0' );
421 }
422 } else {
423 # We do want clients to cache if they can, but they *must* check for updates
424 # on revisiting the page.
425 wfDebug( "** private caching; {$this->mLastModified} **\n", false );
426 header( "Expires: -1" );
427 header( "Cache-Control: private, must-revalidate, max-age=0" );
428 }
429 if($this->mLastModified) header( "Last-modified: {$this->mLastModified}" );
430 } else {
431 wfDebug( "** no caching **\n", false );
432
433 # In general, the absence of a last modified header should be enough to prevent
434 # the client from using its cache. We send a few other things just to make sure.
435 header( 'Expires: -1' );
436 header( 'Cache-Control: no-cache, no-store, max-age=0, must-revalidate' );
437 header( 'Pragma: no-cache' );
438 }
439 }
440
441 /**
442 * Finally, all the text has been munged and accumulated into
443 * the object, let's actually output it:
444 */
445 function output() {
446 global $wgUser, $wgOutputEncoding;
447 global $wgContLanguageCode, $wgDebugRedirects, $wgMimeType, $wgProfiler;
448
449 if( $this->mDoNothing ){
450 return;
451 }
452 $fname = 'OutputPage::output';
453 wfProfileIn( $fname );
454 $sk = $wgUser->getSkin();
455
456 if ( '' != $this->mRedirect ) {
457 if( substr( $this->mRedirect, 0, 4 ) != 'http' ) {
458 # Standards require redirect URLs to be absolute
459 global $wgServer;
460 $this->mRedirect = $wgServer . $this->mRedirect;
461 }
462 if( $this->mRedirectCode == '301') {
463 if( !$wgDebugRedirects ) {
464 header("HTTP/1.1 {$this->mRedirectCode} Moved Permanently");
465 }
466 $this->mLastModified = wfTimestamp( TS_RFC2822 );
467 }
468
469 $this->sendCacheControl();
470
471 if( $wgDebugRedirects ) {
472 $url = htmlspecialchars( $this->mRedirect );
473 print "<html>\n<head>\n<title>Redirect</title>\n</head>\n<body>\n";
474 print "<p>Location: <a href=\"$url\">$url</a></p>\n";
475 print "</body>\n</html>\n";
476 } else {
477 header( 'Location: '.$this->mRedirect );
478 }
479 if ( isset( $wgProfiler ) ) { wfDebug( $wgProfiler->getOutput() ); }
480 wfProfileOut( $fname );
481 return;
482 }
483 elseif ( $this->mStatusCode )
484 {
485 $statusMessage = array(
486 100 => 'Continue',
487 101 => 'Switching Protocols',
488 102 => 'Processing',
489 200 => 'OK',
490 201 => 'Created',
491 202 => 'Accepted',
492 203 => 'Non-Authoritative Information',
493 204 => 'No Content',
494 205 => 'Reset Content',
495 206 => 'Partial Content',
496 207 => 'Multi-Status',
497 300 => 'Multiple Choices',
498 301 => 'Moved Permanently',
499 302 => 'Found',
500 303 => 'See Other',
501 304 => 'Not Modified',
502 305 => 'Use Proxy',
503 307 => 'Temporary Redirect',
504 400 => 'Bad Request',
505 401 => 'Unauthorized',
506 402 => 'Payment Required',
507 403 => 'Forbidden',
508 404 => 'Not Found',
509 405 => 'Method Not Allowed',
510 406 => 'Not Acceptable',
511 407 => 'Proxy Authentication Required',
512 408 => 'Request Timeout',
513 409 => 'Conflict',
514 410 => 'Gone',
515 411 => 'Length Required',
516 412 => 'Precondition Failed',
517 413 => 'Request Entity Too Large',
518 414 => 'Request-URI Too Large',
519 415 => 'Unsupported Media Type',
520 416 => 'Request Range Not Satisfiable',
521 417 => 'Expectation Failed',
522 422 => 'Unprocessable Entity',
523 423 => 'Locked',
524 424 => 'Failed Dependency',
525 500 => 'Internal Server Error',
526 501 => 'Not Implemented',
527 502 => 'Bad Gateway',
528 503 => 'Service Unavailable',
529 504 => 'Gateway Timeout',
530 505 => 'HTTP Version Not Supported',
531 507 => 'Insufficient Storage'
532 );
533
534 if ( $statusMessage[$this->mStatusCode] )
535 header( 'HTTP/1.1 ' . $this->mStatusCode . ' ' . $statusMessage[$this->mStatusCode] );
536 }
537
538 # Buffer output; final headers may depend on later processing
539 ob_start();
540
541 # Disable temporary placeholders, so that the skin produces HTML
542 $sk->postParseLinkColour( false );
543
544 header( "Content-type: $wgMimeType; charset={$wgOutputEncoding}" );
545 header( 'Content-language: '.$wgContLanguageCode );
546
547 if ($this->mArticleBodyOnly) {
548 $this->out($this->mBodytext);
549 } else {
550 wfProfileIn( 'Output-skin' );
551 $sk->outputPage( $this );
552 wfProfileOut( 'Output-skin' );
553 }
554
555 $this->sendCacheControl();
556 ob_end_flush();
557 wfProfileOut( $fname );
558 }
559
560 function out( $ins ) {
561 global $wgInputEncoding, $wgOutputEncoding, $wgContLang;
562 if ( 0 == strcmp( $wgInputEncoding, $wgOutputEncoding ) ) {
563 $outs = $ins;
564 } else {
565 $outs = $wgContLang->iconv( $wgInputEncoding, $wgOutputEncoding, $ins );
566 if ( false === $outs ) { $outs = $ins; }
567 }
568 print $outs;
569 }
570
571 function setEncodings() {
572 global $wgInputEncoding, $wgOutputEncoding;
573 global $wgUser, $wgContLang;
574
575 $wgInputEncoding = strtolower( $wgInputEncoding );
576
577 if( $wgUser->getOption( 'altencoding' ) ) {
578 $wgContLang->setAltEncoding();
579 return;
580 }
581
582 if ( empty( $_SERVER['HTTP_ACCEPT_CHARSET'] ) ) {
583 $wgOutputEncoding = strtolower( $wgOutputEncoding );
584 return;
585 }
586 $wgOutputEncoding = $wgInputEncoding;
587 }
588
589 /**
590 * Returns a HTML comment with the elapsed time since request.
591 * This method has no side effects.
592 * Use wfReportTime() instead.
593 * @return string
594 * @deprecated
595 */
596 function reportTime() {
597 $time = wfReportTime();
598 return $time;
599 }
600
601 /**
602 * Note: these arguments are keys into wfMsg(), not text!
603 */
604 function errorpage( $title, $msg ) {
605 global $wgTitle;
606
607 $this->mDebugtext .= 'Original title: ' .
608 $wgTitle->getPrefixedText() . "\n";
609 $this->setPageTitle( wfMsg( $title ) );
610 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
611 $this->setRobotpolicy( 'noindex,nofollow' );
612 $this->setArticleRelated( false );
613 $this->enableClientCache( false );
614 $this->mRedirect = '';
615
616 $this->mBodytext = '';
617 $this->addWikiText( wfMsg( $msg ) );
618 $this->returnToMain( false );
619
620 $this->output();
621 wfErrorExit();
622 }
623
624 /**
625 * Display an error page indicating that a given version of MediaWiki is
626 * required to use it
627 *
628 * @param mixed $version The version of MediaWiki needed to use the page
629 */
630 function versionRequired( $version ) {
631 global $wgUser;
632
633 $this->setPageTitle( wfMsg( 'versionrequired', $version ) );
634 $this->setHTMLTitle( wfMsg( 'versionrequired', $version ) );
635 $this->setRobotpolicy( 'noindex,nofollow' );
636 $this->setArticleRelated( false );
637 $this->mBodytext = '';
638
639 $this->addWikiText( wfMsg( 'versionrequiredtext', $version ) );
640 $this->returnToMain();
641 }
642
643 /**
644 * Display an error page noting that a given permission bit is required.
645 * This should generally replace the sysopRequired, developerRequired etc.
646 * @param string $permission key required
647 */
648 function permissionRequired( $permission ) {
649 global $wgUser;
650
651 $this->setPageTitle( wfMsg( 'badaccess' ) );
652 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
653 $this->setRobotpolicy( 'noindex,nofollow' );
654 $this->setArticleRelated( false );
655 $this->mBodytext = '';
656
657 $sk = $wgUser->getSkin();
658 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ) );
659 $this->addHTML( wfMsgHtml( 'badaccesstext', $ap, $permission ) );
660 $this->returnToMain();
661 }
662
663 /**
664 * @deprecated
665 */
666 function sysopRequired() {
667 global $wgUser;
668
669 $this->setPageTitle( wfMsg( 'sysoptitle' ) );
670 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
671 $this->setRobotpolicy( 'noindex,nofollow' );
672 $this->setArticleRelated( false );
673 $this->mBodytext = '';
674
675 $sk = $wgUser->getSkin();
676 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
677 $this->addHTML( wfMsgHtml( 'sysoptext', $ap ) );
678 $this->returnToMain();
679 }
680
681 /**
682 * @deprecated
683 */
684 function developerRequired() {
685 global $wgUser;
686
687 $this->setPageTitle( wfMsg( 'developertitle' ) );
688 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
689 $this->setRobotpolicy( 'noindex,nofollow' );
690 $this->setArticleRelated( false );
691 $this->mBodytext = '';
692
693 $sk = $wgUser->getSkin();
694 $ap = $sk->makeKnownLink( wfMsgForContent( 'administrators' ), '' );
695 $this->addHTML( wfMsgHtml( 'developertext', $ap ) );
696 $this->returnToMain();
697 }
698
699 function loginToUse() {
700 global $wgUser, $wgTitle, $wgContLang;
701
702 $this->setPageTitle( wfMsg( 'loginreqtitle' ) );
703 $this->setHTMLTitle( wfMsg( 'errorpagetitle' ) );
704 $this->setRobotpolicy( 'noindex,nofollow' );
705 $this->setArticleFlag( false );
706 $this->mBodytext = '';
707 $loginpage = Title::makeTitle(NS_SPECIAL, 'Userlogin');
708 $sk = $wgUser->getSkin();
709 $loginlink = $sk->makeKnownLinkObj($loginpage, wfMsg('loginreqlink'),
710 'returnto=' . htmlspecialchars($wgTitle->getPrefixedDBkey()));
711 $this->addHTML( wfMsgHtml( 'loginreqpagetext', $loginlink ) );
712
713 # We put a comment in the .html file so a Sysop can diagnose the page the
714 # user can't see.
715 $this->addHTML( "\n<!--" .
716 $wgContLang->getNsText( $wgTitle->getNamespace() ) .
717 ':' .
718 $wgTitle->getDBkey() . '-->' );
719 $this->returnToMain(); # Flip back to the main page after 10 seconds.
720 }
721
722 function databaseError( $fname, $sql, $error, $errno ) {
723 global $wgUser, $wgCommandLineMode, $wgShowSQLErrors;
724
725 $this->setPageTitle( wfMsgNoDB( 'databaseerror' ) );
726 $this->setRobotpolicy( 'noindex,nofollow' );
727 $this->setArticleRelated( false );
728 $this->enableClientCache( false );
729 $this->mRedirect = '';
730
731 if( !$wgShowSQLErrors ) {
732 $sql = wfMsg( 'sqlhidden' );
733 }
734
735 if ( $wgCommandLineMode ) {
736 $msg = wfMsgNoDB( 'dberrortextcl', htmlspecialchars( $sql ),
737 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
738 } else {
739 $msg = wfMsgNoDB( 'dberrortext', htmlspecialchars( $sql ),
740 htmlspecialchars( $fname ), $errno, htmlspecialchars( $error ) );
741 }
742
743 if ( $wgCommandLineMode || !is_object( $wgUser )) {
744 print $msg."\n";
745 wfErrorExit();
746 }
747 $this->mBodytext = $msg;
748 $this->output();
749 wfErrorExit();
750 }
751
752 function readOnlyPage( $source = null, $protected = false ) {
753 global $wgUser, $wgReadOnlyFile, $wgReadOnly;
754
755 $this->setRobotpolicy( 'noindex,nofollow' );
756 $this->setArticleRelated( false );
757
758 if( $protected ) {
759 $this->setPageTitle( wfMsg( 'viewsource' ) );
760 $this->addWikiText( wfMsg( 'protectedtext' ) );
761 } else {
762 $this->setPageTitle( wfMsg( 'readonly' ) );
763 if ( $wgReadOnly ) {
764 $reason = $wgReadOnly;
765 } else {
766 $reason = file_get_contents( $wgReadOnlyFile );
767 }
768 $this->addWikiText( wfMsg( 'readonlytext', $reason ) );
769 }
770
771 if( is_string( $source ) ) {
772 if( strcmp( $source, '' ) == 0 ) {
773 global $wgTitle ;
774 if ( $wgTitle->getNamespace() == NS_MEDIAWIKI ) {
775 $source = wfMsgWeirdKey ( $wgTitle->getText() ) ;
776 } else {
777 $source = wfMsg( $wgUser->isLoggedIn() ? 'noarticletext' : 'noarticletextanon' );
778 }
779 }
780 $rows = $wgUser->getOption( 'rows' );
781 $cols = $wgUser->getOption( 'cols' );
782 $text = "\n<textarea cols='$cols' rows='$rows' readonly='readonly'>" .
783 htmlspecialchars( $source ) . "\n</textarea>";
784 $this->addHTML( $text );
785 }
786
787 $this->returnToMain( false );
788 }
789
790 function fatalError( $message ) {
791 $this->setPageTitle( wfMsg( "internalerror" ) );
792 $this->setRobotpolicy( "noindex,nofollow" );
793 $this->setArticleRelated( false );
794 $this->enableClientCache( false );
795 $this->mRedirect = '';
796
797 $this->mBodytext = $message;
798 $this->output();
799 wfErrorExit();
800 }
801
802 function unexpectedValueError( $name, $val ) {
803 $this->fatalError( wfMsg( 'unexpected', $name, $val ) );
804 }
805
806 function fileCopyError( $old, $new ) {
807 $this->fatalError( wfMsg( 'filecopyerror', $old, $new ) );
808 }
809
810 function fileRenameError( $old, $new ) {
811 $this->fatalError( wfMsg( 'filerenameerror', $old, $new ) );
812 }
813
814 function fileDeleteError( $name ) {
815 $this->fatalError( wfMsg( 'filedeleteerror', $name ) );
816 }
817
818 function fileNotFoundError( $name ) {
819 $this->fatalError( wfMsg( 'filenotfound', $name ) );
820 }
821
822 /**
823 * return from error messages or notes
824 * @param $auto automatically redirect the user after 10 seconds
825 * @param $returnto page title to return to. Default is Main Page.
826 */
827 function returnToMain( $auto = true, $returnto = NULL ) {
828 global $wgUser, $wgOut, $wgRequest;
829
830 if ( $returnto == NULL ) {
831 $returnto = $wgRequest->getText( 'returnto' );
832 }
833 $returnto = htmlspecialchars( $returnto );
834
835 $sk = $wgUser->getSkin();
836 if ( '' == $returnto ) {
837 $returnto = wfMsgForContent( 'mainpage' );
838 }
839 $link = $sk->makeKnownLink( $returnto, '' );
840
841 $r = wfMsg( 'returnto', $link );
842 if ( $auto ) {
843 $titleObj = Title::newFromText( $returnto );
844 $wgOut->addMeta( 'http:Refresh', '10;url=' . $titleObj->escapeFullURL() );
845 }
846 $wgOut->addHTML( "\n<p>$r</p>\n" );
847 }
848
849 /**
850 * This function takes the title (first item of mGoodLinks), categories, existing and broken links for the page
851 * and uses the first 10 of them for META keywords
852 */
853 function addKeywords( &$parserOutput ) {
854 global $wgTitle;
855 $this->addKeyword( $wgTitle->getPrefixedText() );
856 $count = 1;
857 $links2d =& $parserOutput->getLinks();
858 foreach ( $links2d as $ns => $dbkeys ) {
859 foreach( $dbkeys as $dbkey => $id ) {
860 $this->addKeyword( $dbkey );
861 if ( ++$count > 10 ) {
862 break 2;
863 }
864 }
865 }
866 }
867
868 /**
869 * @private
870 * @return string
871 */
872 function headElement() {
873 global $wgDocType, $wgDTD, $wgContLanguageCode, $wgOutputEncoding, $wgMimeType;
874 global $wgUser, $wgContLang, $wgRequest, $wgUseTrackbacks, $wgTitle;
875
876 if( $wgMimeType == 'text/xml' || $wgMimeType == 'application/xhtml+xml' || $wgMimeType == 'application/xml' ) {
877 $ret = "<?xml version=\"1.0\" encoding=\"$wgOutputEncoding\" ?>\n";
878 } else {
879 $ret = '';
880 }
881
882 $ret .= "<!DOCTYPE html PUBLIC \"$wgDocType\"\n \"$wgDTD\">\n";
883
884 if ( '' == $this->getHTMLTitle() ) {
885 $this->setHTMLTitle( wfMsg( 'pagetitle', $this->getPageTitle() ));
886 }
887
888 $rtl = $wgContLang->isRTL() ? " dir='RTL'" : '';
889 $ret .= "<html xmlns=\"http://www.w3.org/1999/xhtml\" xml:lang=\"$wgContLanguageCode\" lang=\"$wgContLanguageCode\" $rtl>\n";
890 $ret .= "<head>\n<title>" . htmlspecialchars( $this->getHTMLTitle() ) . "</title>\n";
891 array_push( $this->mMetatags, array( "http:Content-type", "$wgMimeType; charset={$wgOutputEncoding}" ) );
892
893 $ret .= $this->getHeadLinks();
894 global $wgStylePath;
895 if( $this->isPrintable() ) {
896 $media = '';
897 } else {
898 $media = "media='print'";
899 }
900 $printsheet = htmlspecialchars( "$wgStylePath/common/wikiprintable.css" );
901 $ret .= "<link rel='stylesheet' type='text/css' $media href='$printsheet' />\n";
902
903 $sk = $wgUser->getSkin();
904 $ret .= $sk->getHeadScripts();
905 $ret .= $this->mScripts;
906 $ret .= $sk->getUserStyles();
907
908 if ($wgUseTrackbacks && $this->isArticleRelated())
909 $ret .= $wgTitle->trackbackRDF();
910
911 $ret .= "</head>\n";
912 return $ret;
913 }
914
915 function getHeadLinks() {
916 global $wgRequest, $wgStylePath;
917 $ret = '';
918 foreach ( $this->mMetatags as $tag ) {
919 if ( 0 == strcasecmp( 'http:', substr( $tag[0], 0, 5 ) ) ) {
920 $a = 'http-equiv';
921 $tag[0] = substr( $tag[0], 5 );
922 } else {
923 $a = 'name';
924 }
925 $ret .= "<meta $a=\"{$tag[0]}\" content=\"{$tag[1]}\" />\n";
926 }
927
928 $p = $this->mRobotpolicy;
929 if( $p !== '' && $p != 'index,follow' ) {
930 // http://www.robotstxt.org/wc/meta-user.html
931 // Only show if it's different from the default robots policy
932 $ret .= "<meta name=\"robots\" content=\"$p\" />\n";
933 }
934
935 if ( count( $this->mKeywords ) > 0 ) {
936 $strip = array(
937 "/<.*?>/" => '',
938 "/_/" => ' '
939 );
940 $ret .= "<meta name=\"keywords\" content=\"" .
941 htmlspecialchars(preg_replace(array_keys($strip), array_values($strip),implode( ",", $this->mKeywords ))) . "\" />\n";
942 }
943 foreach ( $this->mLinktags as $tag ) {
944 $ret .= '<link';
945 foreach( $tag as $attr => $val ) {
946 $ret .= " $attr=\"" . htmlspecialchars( $val ) . "\"";
947 }
948 $ret .= " />\n";
949 }
950 if( $this->isSyndicated() ) {
951 # FIXME: centralize the mime-type and name information in Feed.php
952 $link = $wgRequest->escapeAppendQuery( 'feed=rss' );
953 $ret .= "<link rel='alternate' type='application/rss+xml' title='RSS 2.0' href='$link' />\n";
954 $link = $wgRequest->escapeAppendQuery( 'feed=atom' );
955 $ret .= "<link rel='alternate' type='application/atom+xml' title='Atom 0.3' href='$link' />\n";
956 }
957
958 return $ret;
959 }
960
961 /**
962 * Turn off regular page output and return an error reponse
963 * for when rate limiting has triggered.
964 * @todo i18n
965 * @access public
966 */
967 function rateLimited() {
968 global $wgOut;
969 $wgOut->disable();
970 wfHttpError( 500, 'Internal Server Error',
971 'Sorry, the server has encountered an internal error. ' .
972 'Please wait a moment and hit "refresh" to submit the request again.' );
973 }
974
975 }
976
977 } // MediaWiki
978
979 ?>